# 采购合同
from api_setting import *
from dim_api_auto_test.DimAPI.core.request_client import RestClient
from dim_api_auto_test.DimAPI.search import Search
from tools.database import MySQLClient


class PurchaseContract(RestClient):
    def __init__(self, **kwargs):
        super(PurchaseContract, self).__init__(**kwargs)
        self.search = Search()
        self.mysql = MySQLClient.setup()

    def delete_purchase_contract(self):
        """
        删除单份采购合同
        :return:
        """
        url = DELETE_PURCHASE_CONTRACT_URL
        # 查询草稿状态的合同，并在查询结果中获取第1份合同的id
        pc_id = self.search.search_purchase_contract_by_status("Draft")["data"]["list"][
            0
        ]["id"]
        params = {"ids": pc_id}
        r = self.get(url=url, params=params)
        return r.content

    def send_back_purchase_contract(self):
        """
        退回待审批的采购合同
        :return:
        """
        url = UPDATE_CONTRACT_STATUS_URL
        # 查询已提交待采购的采购合同，并获取第1份合同的单号
        contractNo = self.search.search_purchase_contract_by_status("Submit")["data"][
            "list"
        ][0]["contractNo"]
        params = {
            "contractNo": contractNo,
            "status": "Draft",
            "returnReason": "测试采购合同退回",
        }
        r = self.post(url=url, params=params)
        return r.content

    def audit_fail_purchase_contract(self):
        """
        采购合同审核不通过
        :return:
        """
        url = UPDATE_CONTRACT_STATUS_URL
        # 查询已提交待采购的采购合同，并获取第1份合同的单号
        contractNo = self.search.search_purchase_contract_by_status("Submit")["data"][
            "list"
        ][0]["contractNo"]
        params = {
            "contractNo": contractNo,
            "status": "Invalid",
            "returnReason": "测试采购合同 审核不通过",
        }
        r = self.post(url=url, params=params)
        return r.content


if __name__ == "__main__":
    test = PurchaseContract()
    res = test.audit_fail_purchase_contract()
    print(res)
